package com.ktg.mes.wm.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.constant.UserConstants;
import com.ktg.common.core.domain.entity.SysDept;
import com.ktg.common.utils.DateUtils;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.common.utils.StringUtils;
import com.ktg.mes.unit.sql.service.SQLToolService;
import com.ktg.mes.unit.sync.SyncModel;
import com.ktg.mes.unit.sync.service.IFromService;
import com.ktg.mes.wm.domain.WmStorageArea;
import com.ktg.mes.wm.domain.WmStorageLocation;
import com.ktg.mes.wm.mapper.WmStorageAreaMapper;
import com.ktg.mes.wm.mapper.WmStorageLocationMapper;
import com.ktg.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.wm.mapper.WmWarehouseMapper;
import com.ktg.mes.wm.domain.WmWarehouse;
import com.ktg.mes.wm.service.IWmWarehouseService;

/**
 * 仓库设置Service业务层处理
 *
 * @author yinjinlu
 * @date 2022-05-07
 */
@Service
public class WmWarehouseServiceImpl  extends ServiceImpl<WmWarehouseMapper, WmWarehouse>  implements IWmWarehouseService
{


    @Autowired
    private WmStorageLocationMapper wmStorageLocationMapper;

    @Autowired
    private WmStorageAreaMapper wmStorageAreaMapper;
    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private IFromService fromService;

    @Autowired
    private SQLToolService toIService;


    /**
     * 查询仓库设置
     *
     * @param warehouseId 仓库设置主键
     * @return 仓库设置
     */
    @Override
    public WmWarehouse selectWmWarehouseByWarehouseId(Long warehouseId)
    {
        return getBaseMapper().selectWmWarehouseByWarehouseId(warehouseId);
    }

    @Override
    public WmWarehouse selectWmWarehouseByWarehouseCode(String warehouseCdoe) {
        return getBaseMapper().selectWmWarehouseByWarehouseCode(warehouseCdoe);
    }

    /**
     * 查询仓库设置列表
     *
     * @param wmWarehouse 仓库设置
     * @return 仓库设置
     */
    @Override
    public List<WmWarehouse> selectWmWarehouseList(WmWarehouse wmWarehouse)
    {
        List<WmWarehouse> wmWarehouses = getBaseMapper().selectWmWarehouseList(wmWarehouse);
        for (WmWarehouse warehouse : wmWarehouses) {
            if(warehouse.getDeptId() != null){
                SysDept sysDept = deptService.selectDeptById(warehouse.getDeptId());
                warehouse.setDeptName(sysDept.getDeptName());
            }
        }
        // return wmWarehouseMapper.selectWmWarehouseList(wmWarehouse);
        return wmWarehouses;
    }

    @Override
    public List<WmWarehouse> getTreeList() {
        return getBaseMapper().getTreeList();
    }

    @Override
    public String checkWarehouseCodeUnique(WmWarehouse wmWarehouse) {
        WmWarehouse warehouse = getBaseMapper().checkWarehouseCodeUnique(wmWarehouse);
        Long warehouseId = wmWarehouse.getWarehouseId()==null?-1L:wmWarehouse.getWarehouseId();
        if(StringUtils.isNotNull(warehouse) && warehouse.getWarehouseId().longValue() != warehouseId.longValue()){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkWarehouseNameUnique(WmWarehouse wmWarehouse) {
        WmWarehouse warehouse = getBaseMapper().checkWarehouseNameUnique(wmWarehouse);
        Long warehouseId = wmWarehouse.getWarehouseId()==null?-1L:wmWarehouse.getWarehouseId();
        if(StringUtils.isNotNull(warehouse) && warehouse.getWarehouseId().longValue() != warehouseId.longValue()){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增仓库设置
     *
     * @param wmWarehouse 仓库设置
     * @return 结果
     */
    @Override
    public boolean insertWmWarehouse(WmWarehouse wmWarehouse)
    {
        wmWarehouse.setCreateBy(SecurityUtils.getUsername());
        wmWarehouse.setCreateTime(DateUtils.getNowDate());
        return save(wmWarehouse);
    }

    /**
     * 修改仓库设置
     *
     * @param wmWarehouse 仓库设置
     * @return 结果
     */
    @Override
    public boolean updateWmWarehouse(WmWarehouse wmWarehouse)
    {
        wmWarehouse.setUpdateTime(DateUtils.getNowDate());
        wmWarehouse.setUpdateBy(SecurityUtils.getUsername());
        return updateById(wmWarehouse);
    }

    /**
     * 批量删除仓库设置
     *
     * @param warehouseIds 需要删除的仓库设置主键
     * @return 结果
     */
    @Override
    public int deleteWmWarehouseByWarehouseIds(Long[] warehouseIds)
    {
        return getBaseMapper().deleteWmWarehouseByWarehouseIds(warehouseIds);
    }

    /**
     * 删除仓库设置信息
     *
     * @param warehouseId 仓库设置主键
     * @return 结果
     */
    @Override
    public int deleteWmWarehouseByWarehouseId(Long warehouseId)
    {
        return getBaseMapper().deleteWmWarehouseByWarehouseId(warehouseId);
    }

    @Override
    public WmWarehouse initVirtualWarehouse() {
        WmWarehouse warehouse = new WmWarehouse();
        warehouse.setWarehouseCode(UserConstants.VIRTUAL_WH);
        warehouse.setWarehouseName("线边库-虚拟");
        getBaseMapper().insertWmWarehouse(warehouse);

        WmStorageLocation location = new WmStorageLocation();
        location.setWarehouseId(warehouse.getWarehouseId());
        location.setLocationCode(UserConstants.VIRTUAL_WS);
        location.setLocationName("线边库库区-虚拟");
        location.setAreaFlag(UserConstants.YES);
        wmStorageLocationMapper.insertWmStorageLocation(location);

        WmStorageArea area = new WmStorageArea();
        area.setLocationId(location.getLocationId());
        area.setAreaCode(UserConstants.VIRTUAL_WA);
        area.setAreaName("线边库库位-虚拟");
        wmStorageAreaMapper.insertWmStorageArea(area);
        return warehouse;
    }



    /**
     * 同步库存
     */
    public void sysnStock(){
        SyncModel syncModel = new SyncModel();
        syncModel.setService(fromService,toIService);
        syncModel.setTable("Warehouse","wm_warehouse");
        syncModel.setSyncData("cBarCode","CONVERT(BIGINT,pubufts)","warehouse_id","pubufts");
        syncModel.addColumn("cWhCode","warehouse_id", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("cBarCode","warehouse_code", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("cWhName","warehouse_name", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("cWhAddress","location", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("cWhPerson","charge", SyncModel.SyncModelType.TypeString);
        syncModel.addColumn("CONVERT(BIGINT,pubufts)","pubufts", SyncModel.SyncModelType.TypeInteger);
        syncModel.synsStart("仓库信息");
    }

}
